package com.example.lenovo.a203_yhy_ordering_3.model;

import com.example.lenovo.a203_yhy_ordering_3.beans.UserInfoBean;
import com.example.lenovo.a203_yhy_ordering_3.iface.UserInfoIface;
import com.example.lenovo.a203_yhy_ordering_3.listener.UserInfoListener;
import com.example.lenovo.a203_yhy_ordering_3.service.UserInfoService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by lenovo on 2018/12/16.
 */

public class UserInfoModel implements UserInfoIface{
    private Retrofit retrofit;

    public UserInfoModel(){
        retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }


    @Override
    public void getUserInfoResult(int user_id, final UserInfoListener userInfoListener) {
        UserInfoService userInfoService = retrofit.create(UserInfoService.class);
        Call<UserInfoBean> userInfoBeanCall = userInfoService.getUserById(user_id);
        userInfoBeanCall.enqueue(new Callback<UserInfoBean>() {
            @Override
            public void onResponse(Call<UserInfoBean> call, Response<UserInfoBean> response) {
                if(response!=null)
                    userInfoListener.onResponse(response.body());
                else
                    userInfoListener.onFail("获取个人信息失败");
            }

            @Override
            public void onFailure(Call<UserInfoBean> call, Throwable t) {
                userInfoListener.onFail(t.toString());
            }
        });
    }
}
